<?php
error_reporting(0);
set_time_limit(0);
$phpPath = dirname(dirname(__FILE__));
$aNum = FALSE;
include_once($phpPath."/hip.php" );
include_once($phpPath."/config.php" );
include_once($phpPath."/function.php" );
include_once($phpPath."/class/functions.php" );
include_once($phpPath."/class/user.php" );
include_once($phpPath."/class/db.php" );
include_once($phpPath."/class/config.php" );
include_once($phpPath."/class_3d/class.lotto3d.php" );
include_once($phpPath."/class_5h/class.lotto5h.php" );
include_once($phpPath."/class_3d/MyLotto_3d.php" );
include_once($phpPath."/class_5h/MyLotto_5h.php" );
include_once($phpPath."/class_3d/RateConfig_3d.php" );
include_once($phpPath."/class_5h/RateConfig_5h.php" );
include_once($phpPath."/class_3d/postBetToXBet_3d.php" );

function curl_get_file_contents($gametype , $URL)
{
    $jieguo = array ( );
    $c = curl_init();
    curl_setopt($c , CURLOPT_TIMEOUT , 10);
    curl_setopt($c , CURLOPT_RETURNTRANSFER , 1);
    curl_setopt($c , CURLOPT_FOLLOWLOCATION , 1);
    curl_setopt($c , CURLOPT_URL , $URL);
    curl_setopt($c , CURLOPT_HTTPHEADER , array ( "Host: 122.112.119.88:98" ));
    curl_setopt($c , CURLOPT_ENCODING , "gzip");
    $contents = curl_exec($c);
    curl_close($c);
    if ( $contents )
    {
        $json = json_decode($contents , TRUE);
        if ( is_array($json) )
        {
            foreach ( $json as $key => $value )
            {
                $ny = explode("," , $value['Num']);
                switch ( $gametype )
                {
                    case 10:
                        if ( count($ny) == 3 )
                        {
                            $jieguo[] = array (
                                "qs" => sprintf("%02d" , $value['periodsNumber']) ,
                                "open" => substr($value['periodsTime'] , 0 , 4).'-'.substr($value['periodsTime'] , 4 , 2).'-'.substr($value['periodsTime'] , 6 , 2) ,
                                "open_time" => $value['drawTime'] ,
                                "code" => array ( $ny[2] , $ny[1] , $ny[0] ) ,
                                "sql" => "n3!=".intval($ny[0])." or n2!=".intval($ny[1])." or n1!=".intval($ny[2]).""
                            );
                        }
                        break;
                    case 11:
                        if ( count($ny) == 5 )
                        {
                            $jieguo[] = array (
                                "qs" => $value['periodsTime'].'-'.sprintf("%03d" , $value['periodsNumber']) ,
                                "open" => substr($value['periodsTime'] , 0 , 4).'-'.substr($value['periodsTime'] , 4 , 2).'-'.substr($value['periodsTime'] , 6 , 2) ,
                                "open_time" => $value['drawTime'] ,
                                "code" => array ( $ny[4] , $ny[3] , $ny[2] , $ny[1] , $ny[0] ) ,
                                "sql" => "n5!=".intval($ny[0])." or n4!=".intval($ny[1])." or n3!=".intval($ny[2])." or n2!=".intval($ny[3])." or n1!=".intval($ny[4]).""
                            );
                        }
                    case 12:
                    case 13:
                    case 14:
                    case 15:
                        if ( count($ny) == 5 )
                        {
                            $jieguo[] = array (
                                "qs" => substr($value['periodsTime'] , 4 , 2).substr($value['periodsTime'] , 6 , 2).sprintf("%03d" , $value['periodsNumber']) ,
                                "open" => substr($value['periodsTime'] , 0 , 4).'-'.substr($value['periodsTime'] , 4 , 2).'-'.substr($value['periodsTime'] , 6 , 2) ,
                                "open_time" => $value['drawTime'] ,
                                "code" => array ( $ny[4] , $ny[3] , $ny[2] , $ny[1] , $ny[0] ) ,
                                "sql" => "n5!=".intval($ny[0])." or n4!=".intval($ny[1])." or n3!=".intval($ny[2])." or n2!=".intval($ny[3])." or n1!=".intval($ny[4]).""
                            );
                        }
                        break;
                    case 20:
                        if ( count($ny) == 5 )
                        {
                            $jieguo[] = array (
                                "qs" => $value['periodsTime'].'-'.sprintf("%02d" , $value['periodsNumber']) ,
                                "open" => substr($value['drawTime'] , 0 , 10) ,
                                "open_time" => $value['drawTime'] ,
                                "code" => array ( $ny[0] , $ny[1] , $ny[2] , $ny[3] , $ny[4] ) ,
                                "sql" => "n5!=".intval($ny[4])." or n4!=".intval($ny[3])." or n3!=".intval($ny[2])." or n2!=".intval($ny[1])." or n1!=".intval($ny[0]).""
                            );
                        }
                        break;
                    case 21:
                    case 22:
                    case 23:
                        if ( count($ny) == 8 )
                        {
                            $jieguo[] = array (
                                "qs" => $value['periodsTime'].'-'.sprintf("%02d" , $value['periodsNumber']) ,
                                "open" => substr($value['periodsTime'] , 0 , 4).'-'.substr($value['periodsTime'] , 4 , 2).'-'.substr($value['periodsTime'] , 6 , 2) ,
                                "open_time" => $value['drawTime'] ,
                                "code" => array ( $ny[0] , $ny[1] , $ny[2] , $ny[3] , $ny[4] , $ny[5] , $ny[6] , $ny[7] ) ,
                                "sql" => "n8!=".intval($ny[7])." or n7!=".intval($ny[6])." or n6!=".intval($ny[5])." or n5!=".intval($ny[4])." or n4!=".intval($ny[3])." or n3!=".intval($ny[2])." or n2!=".intval($ny[1])." or n1!=".intval($ny[0]).""
                            );
                        }
                        break;
                    case 24:
                        if ( count($ny) == 10 )
                        {
                            $jieguo[] = array (
                                "qs" => $value['periodsNumber'] ,
                                "open" => substr($value['drawTime'] , 0 , 10) ,
                                "open_time" => $value['drawTime'] ,
                                "code" => array ( $ny[0] , $ny[1] , $ny[2] , $ny[3] , $ny[4] , $ny[5] , $ny[6] , $ny[7] , $ny[8] , $ny[9] ) ,
                                "sql" => "n10!=".intval($ny[9])." or n9!=".intval($ny[8])." or n8!=".intval($ny[7])." or n7!=".intval($ny[6])." or n6!=".intval($ny[5])." or n5!=".intval($ny[4])." or n4!=".intval($ny[3])." or n3!=".intval($ny[2])." or n2!=".intval($ny[1])." or n1!=".intval($ny[0]).""
                            );
                        }
                        break;
                }
            }
        }
        if ( count($jieguo) == 0 )
        {
            return FALSE;
        }
        return $jieguo;
    }
    return FALSE;
}

$nowTime = date("Hi");
$game_Arr = $g_gametype_3d;
unset($game_Arr[0]);
unset($game_Arr[1]);
unset($game_Arr[2]);
$game_Arr[10]['cid'] = 1;
$game_Arr[11]['cid'] = 2;
$game_Arr[12]['cid'] = 3;
$game_Arr[13]['cid'] = 4;
$game_Arr[14]['cid'] = 5;
$game_Arr[15]['cid'] = 6;
$game_Arr[20]['cid'] = 7;
$game_Arr[21]['cid'] = 8;
$game_Arr[22]['cid'] = 9;
$game_Arr[23]['cid'] = 10;
$game_Arr[24]['cid'] = 11;
foreach ( $game_Arr as $gametype => $tbl_Arr )
{
    $tbl = "_".$tbl_Arr['en'];
    if ( ! $g_menu_enabled[$gametype] )
    {
        continue;
    }
    $_8hFlag = false;
    $_happyFlag = false;
    if ( $tbl_Arr['type'] == 3 )
    {
        $_happyFlag = true;
        $Show5NFlag = true;
        if ( $tbl == "_5h" )
        {
            $sqlN5Add = "(n5+n4+n3+n2+n1)=-5";
        }
        else if ( $gametype == 24 )
        {
            $sqlN5Add = "(n10+n9+n8+n7+n6+n5+n4+n3+n2+n1)=-10";
            $_8hFlag = true;
        }
        else
        {
            $sqlN5Add = "(n8+n7+n6+n5+n4+n3+n2+n1)=-8";
            $_8hFlag = true;
        }
        $MyLotto_3d = new MyLotto_5h($gametype);
    }
    else
    {
        $Show5NFlag = getShow35($gametype);
        if ( $Show5NFlag )
        {
            $sqlN5Add = "(n5+n4+n3+n2+n1)=-5";
            $MyLotto_3d = new MyLotto_3d_5n($gametype);
        }
        else
        {
            $sqlN5Add = "(n3+n2+n1)=-3";
            $MyLotto_3d = new MyLotto_3d($gametype);
        }
    }
    $ntime = date("Hi");
    $ymdhi = date("ymdHis");
    $err = 0;
    switch ( $gametype )
    {
        case 10 :
            if ( "0000" < $ntime && $ntime < "0950" )
            {
                $err = 1;
            }
            break;
        case 12 :
        case 14 :
            if ( "0215" < $ntime && $ntime < "0940" )
            {
                $err = 1;
            }
            break;
        default :
            if ( "0000" < $ntime && $ntime < "0850" )
            {
                $err = 1;
            }
            break;
    }
    if ( $err == 1 )
    {
        echo date("Y-m-d H;i:s")." ID:{$gametype } TIME NO\n";
        continue;
    }
    $rs = getdb("select value from k_config where id = 'systemcheck{$tbl}' LIMIT 1");
    if ( intval($rs->f[0]) == 1 )
    {
        echo date("Y-m-d H;i:s")." ID:{$gametype } STOP NO\n";
        continue;
    }
    $rs = getdb("select value from k_config where id = 'lotto_auto{$tbl}' LIMIT 1");
    if ( intval($rs->f[0]) != 1 )
    {
        echo date("Y-m-d H;i:s")." ID:{$gametype } AUTO NO\n";
        continue;
    }
    $MyLotto_3d->autoInserLotto();
    $rs = getdb("select openbet from lotto{$tbl} where (date(openbet) between DATE_SUB(curdate(), INTERVAL 1 DAY) and curdate()) and {$sqlN5Add} Order by openbet LIMIT 1");
    if ( $rs->eof )
    {
        echo date("Y-m-d H;i:s")." ID:{$gametype } openbet NO\n";
        continue;
    }
    $otime = date("ymdHis" , strtotime($rs->f[0]));
	
    if ( $ymdhi <= $otime )
    {
        continue;
    }
	
    $JieGuoaNum = curl_get_file_contents($gametype , "http://122.112.119.88:98/API.ashx?id={$tbl_Arr['cid']}");
	if ( ! $JieGuoaNum )
    {
        echo date("Y-m-d H;i:s")." ID:{$gametype } JieGuo NO\n";
        continue;
    }
    foreach ( $JieGuoaNum as $aNum )
    {
        $n1 = isset($aNum["code"][0])?intval($aNum["code"][0]):-1;
        $n2 = isset($aNum["code"][1])?intval($aNum["code"][1]):-1;
        $n3 = isset($aNum["code"][2])?intval($aNum["code"][2]):-1;
        $n4 = isset($aNum["code"][3])?intval($aNum["code"][3]):-1;
        $n5 = isset($aNum["code"][4])?intval($aNum["code"][4]):-1;
        $n6 = isset($aNum["code"][5])?intval($aNum["code"][5]):-1;
        $n7 = isset($aNum["code"][6])?intval($aNum["code"][6]):-1;
        $n8 = isset($aNum["code"][7])?intval($aNum["code"][7]):-1;
        $n9 = isset($aNum["code"][8])?intval($aNum["code"][8]):-1;
        $n10 = isset($aNum["code"][9])?intval($aNum["code"][9]):-1;
        $open = $aNum["open"];
        if ( ($gametype == 12 || $gametype == 14) && date("H:i" , strtotime($aNum['open_time'])) <= "02:00" )
        {
            $open = date("Y-m-d" , strtotime($aNum['open_time']) - 86400);
        }
        $rs = getdb("select id from lotto{$tbl} where open='{$open}' and round='{$aNum["qs"]}' and ({$sqlN5Add} or {$aNum["sql"]}) LIMIT 1");
        if ( ! $rs->eof )
        {
            $id = $rs->f['id'];
            if ( $MyLotto_3d->checkOpen($n3 , $n2 , $n1 , $n4 , $n5 , $n6 , $n7 , $n8) )
            {
                $updateData = array ( );
                $updateData = $MyLotto_3d->calOpenNum($n3 , $n2 , $n1 , $n4 , $n5 , $n6 , $n7 , $n8 , $n9 , $n10);
                if ( $gametype == 24 )
                {
                    $updateData['n10'] = $n10;
                    $updateData['n9'] = $n9;
                }
                if ( $_8hFlag )
                {
                    $updateData['n8'] = $n8;
                    $updateData['n7'] = $n7;
                    $updateData['n6'] = $n6;
                }
                $updateData['n5'] = $n5;
                $updateData['n4'] = $n4;
                $updateData['n3'] = $n3;
                $updateData['n2'] = $n2;
                $updateData['n1'] = $n1;
                $updateData['state'] = "1";
                $updateData['calcbet'] = "1";
                $MyLotto_3d->update($updateData , $id);
                getdb("update k_config set value = 1 Where id = 'calcbet{$tbl}' LIMIT 1");
                if ( $_happyFlag )
                {
                    $lotto3d = new lotto5h($gametype);
                }
                else
                {
                    $lotto3d = new lotto3d($gametype);
                }
                $lotto_id = $lotto3d->CheckLottoOpen($id);
                if ( $lotto_id == $id )
                {
                    $postTime = "0300";
                    $rsp = getdb("select value from k_config where id = 'paper_post_bet' LIMIT 1");
                    if ( ! $rsp->eof )
                    {
                        $postTime = strval($rsp->f[0]);
                    }
                    if ( $postTime <= $nowTime && $open < date("Y-m-d") )
                    {
                        $lotto3d->OpenProcess($lotto_id , "x");
                    }
                    else
                    {
                        $lotto3d->OpenProcess($lotto_id);
                    }
                    db_update("lotto{$tbl}" , array ( "calcbet" => "0" ) , "id={$lotto_id}");
                }
                else
                {
                    echo date("Y-m-d H;i:s")." ID:{$gametype } KaiJiang NO\n";
                    continue;
                }
                getdb("update k_config set value = 0 Where id = 'calcbet{$tbl}' LIMIT 1");
                echo date("Y-m-d H;i:s")." ID:{$gametype } KaiJiang OK\n";
            }
        }
    }
}

if ( "0229" < $ntime && $ntime < "0800" )
{
    $postTime = "0300";
    $rs = getdb("select value from k_config where id = 'paper_post_bet' LIMIT 1");
    if ( $rs && ! $rs->eof )
    {
        $postTime = strval($rs->f[0]);
    }
    $postFlag = 0;
    $rs = getdb("select value from k_config where id = 'paper_post_flag' LIMIT 1");
    if ( $rs && ! $rs->eof )
    {
        $postFlag = intval($rs->f[0]);
    }
    if ( $nowTime == $postTime && $postFlag == 0 )
    {
        include_once($phpPath."/class_3d/Amounts_float_3d.php" );
        $AmountFloat3D = new Amounts_float_3d( );
        $AmountFloat3D->countBet(0 , "3s3" , date("Y-m-d" , strtotime("-1 days")));
        echo date("Y-m-d H;i:s")." postBetToXBet() - begin\n";
        postBetToXBet();
        echo date("Y-m-d H;i:s")." postBetToXBet() - end\n";
        getdb("TRUNCATE TABLE k_user_bet");
        getdb("TRUNCATE TABLE k_sessions");
    }
    //sleep(120);
}
?>
<script>
    function reflesh(){
		document.location.reload();
	}
	setTimeout("reflesh()",5*1000);
</script>